<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Guest_c extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('guest_m');
    }

    public function index() {
        $data['content'] = 'pages/guest/choose-designs';
        $data['title'] = 'Guest';
        $data['current_page'] = 'events-guest-page';
        $data['class'] = '';
        $data['back_to'] = 'guest';
        $data['pics'] = $this->guest_m->get_pics();
        $this->load->view('includes/template', $data);
    }

    function upload_design() {
        if ($_FILES['design-img']['name']) {
                if (!$_FILES['design-img']['error']) {
                    //$ext = end(explode(".", $_FILES['design-img']['name']));
                    $data['imgLoc'] = "uploads/gdesign/";
                    move_uploaded_file($_FILES['design-img']['tmp_name'], $data['imgLoc'] . $_FILES['design-img']['name']);
                    $this->guest_m->save_design_path($data['imgLoc'] . $_FILES['design-img']['name'],1);
                } else {
                    echo "invalid file";
                }
            }
    }
    
    function get_design_pics(){
        $picture= "";
        $query = $this->guest_m->get_pics();
        foreach ($query->result() as $row) { 
            $picture = $picture . '<picture href="#"><img class="pic" src="'.site_url() . $row->design_path . '"/></picture>';
         }
         echo json_encode($picture);
    }
    
    function get_event_guest(){
        $tmp = "";
        $query = $this->guest_m->get_guests($this->input->post("ev-id"));
        foreach ($query->result() as $row) {
            if($row->sent == "yes"){
                $sent = '<td><span  class="glyphicon glyphicon-ok"/><span  style="visibility: hidden">Y</span></td>';
            }else{
                $sent = '<td><span class="glyphicon glyphicon-remove"/><span style="visibility:hidden">N</span></td>';
            }
            
            if($row->rsvp == "yes"){
                
            }else if($row->rsvp == "no"){
                
            }else{
                
            }
            
            if($row->token == "token"){
                
            }else{
                
            }
            $tmp = $tmp . '<tr>
                <td><input type="checkbox" name="chk[]" id="chkbox" name="guest_id" value="' . $row->guest_id . '" class="chkbox" value="1" />'
                .$sent.'<td><span class="span_name">' . $row->g_name . '</span><input type="text" class="form-control edit_name hide" name="gname" value="' . $row->g_name . '"/><span style="visibility: hidden">' . $row->g_name . '</span></td>'.
                '<td><span class="span_gemail">' . $row->g_email . '</span><input  type="text" class="form-control edit_gemail hide" name="g_email" value="' . $row->g_email . '"/><span style="visibility: hidden">' . $row->g_email . '</span></td>'.
                '<td><span class="span_grole">' . $row->g_role . '</span>' . '<input type="text" class="form-control edit_grole hide" name="grole" value="' . $row->g_role . '"/><span style="visibility: hidden">' . $row->g_role . '</span>'.
                '<td>' . '<span class="span_rsvp">' . $item ['rsvp'] . '</span>' . '<select id="rsvp" class="form-control hide edit_rsvp" name="rsvp" >';
                    
        }
        
        echo json_encode($tmp);
//                        echo '<td>' . '<span class="span_rsvp">' . $item ['rsvp'] . '</span>' . '<select id="rsvp" class="form-control hide edit_rsvp" name="rsvp" >';
//                        if ($item ['rsvp'] == 'YES') {
//                            echo '<option selected>YES</option>
//                                <option>NO</option>
//                                <option>MAYBE</option>';
//                        } else if ($item ['rsvp'] == 'MAYBE') {
//                            echo '<option>YES</option>
//                                <option>NO</option>
//                                <option selected>MAYBE</option>';
//                        } else {
//                            echo '<option>YES</option>
//                                <option selected>NO</option>
//                                <option>MAYBE</option>';
//                        }
//                        echo '</select>';
//                        echo '<td>' . '<span class="span_token">'. $item ['token'] . '</span>' . '<select id="token" class="form-control hide edit_token" name="token" >';
//                        if ($item ['token'] == 'YES') {
//                            echo '<option selected>YES</option>
//                                <option>NO</option>';
//                        } else {
//                            echo '<option>YES</option>
//                                <option selected>NO</option>';
//                        }
//                        echo '</select>';
//                        echo '<td>' . '<button  name="edit_btn" title="Edit row" value="' . $item['guest_id'] . '" class="btn btn-info btn-xs edit_btn"><span class="glyphicon glyphicon-edit"></span></button>';
//
//                        echo '</tr>';
//                    }

    }
    
    function send_invitation() {
        $config = Array(
            'protocol' => 'smtp',
            'smtp_host' => 'ssl://smtp.gmail.com',
            'smtp_port' => 465,
            'smtp_user' => 'bbem.guest@gmail.com',
            'smtp_pass' => 'guestmanagement',
            'mailtype' => 'html',
            'charset' => 'iso-8859-1'
        );

        $this->load->library('email', $config);
        $this->email->set_newline("\r\n");
        $this->email->from('bbem.guest@gmail.com', 'BBEM Guest Management');
        //$this->load->model("guest_m");
        $query = $this->guest_m->get_email($this->input->post("ev-id"));

        $tempCon = "";
        foreach ($query->result() as $row) {
            $tempCon = $tempCon . $row->g_email . ",";
        }
        $this->email->to($tempCon);
        $this->email->subject('Invitation');
        $this->email->message('
	 <html>
	 <head>
	 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	 </head>
	 <body>
	 <style>
	 #invi_con{
		
		margin:auto;
		border: 1px solid #000;
		width: 90%;
		padding: 2%;
		margin-top:50px;
		margin-bottom:50px;
		clear:both;
	 }
	 #inner_invi_con{
		margin:auto;
		width: 100%;
	 }
	 #greetings{
		border: 1px solid #000;
		width: 96%;
		height: 150px;
		padding: 10px
	 }
	 #view_full{
		border: 1px solid #000;
		margin: auto;
		width: 200px;
		height: 20px;
		text-align: center;
		background-color: #ccc;
		margin-top: 20px;
		padding: 15px;
	 }
	 #second_con{
		margin-top:20px
	 }
	 #left_con{
		border: 1px solid #000;
		float: left;
		width: 62%;
		border-top:none;
		clear:both;
	 }
	 #right_con{
		border: 1px solid #000;
		
		float:right;
		width:180px;
		text-align: center;
		padding: 10px;
	 }

	 .header{
		margin-top:0px;
		margin-bottom: 0px;
		border-bottom: 1px solid #000;
		border-top: 1px solid #000;
		width: 98.5%;
		background-color: #ccc;
		padding-left:5px;
	 }
	 .p{
		margin-left: 40px;
		margin-top:0px;
		margin-right: 10px;
		font-weight: normal;
	 }
	 h4{
		margin-bottom: 0px;
		padding-left: 5px;
	 }
	 #msg{
		border: 1px solid #000;
		float: right;
		width: 180px;;
		margin-top: 5px;
		padding: 10px;
	 }
	 
	 </style>
	 
	 <div id="container" style="width:600px; margin:auto;">
	 <div id="invi_con">
		<div id="inner_invi_con">
			<div id="greetings">
				<h3>Dear Guest,</h3>
				<p>
					You are invited on this event, you can view the full invitation by clicking the link below.
				</p>
				<div id="view_full">	
					<a href="http://google.com" style="font-weight: bold; color: blue; ">Update RSVP</a>
				</div>
			</div>
			<div style="clear:both"></div>
			<div id="second_con">
				<div id="left_con">
					<div id="invi_details"> 
						<h3 class="header">Invitation details<h3>
						<h4>Title:</h4>
							<p class="p">' . "name ng event"/*$data["event_name"]*/ . '</p>
						<h4>Host by:</h4>
							<p class="p">' . "kliyente"/*$data["client_fname"]*/ . '</p>
						<h4>Description:</h4>
							<p class="p"  >'. "desc"/*$data["event_description"]*/ . '</p>
					</div>
					<div id="time" style="margin-top: 10px;"> 
					<h3 class="header">Time and date<h3>
						<p class="p">' . "time" . ' </p>
					</div>
					<div id="location">
					<h3 class="header">location<h3>
						<p class="p">' . "lokasyon". '</p>
					</div>
				</div>
				<div >
				<div id="right_con">
					<p >You may call, text or email us to update your RSVP for this Event.</p>
					<h3>Contact Us:</h3>
					<h4>09209514178 <br/> benchbacani@yahoo.com</h4>
				</div>
					<div id="msg">
						<p>
						This email is customized for you. Please do not forward it.
						</p>
					</div>
					<div style="clear:both;"></div>
			</div>
			<div>
		</div>
	 </div>
	 </div>
	 </body>
	 </html> ');
        $this->email->send();
        echo json_encode($tempCon);
//        $this->load->model("guest_model");
//        $query = $this->guest_m->get_email();

//        foreach ($query->result() as $row) {
//            $this->email->to($row->g_email);
//            if ($this->email->send()) {
//                echo 'invites sent';
//            } else {
//
//                echo $this->email->print_debugger();
//            }
//        }
//
//        $d3 = Array(
//            'sent' => 'yes',
//        );
//        foreach ($query->result() as $row) {
//            $id = $row->guest_id;
//            $query = $this->guest_model->update_guest_sent($d3, $id);
//        }
    }
    
    function insert_guest() {
        $gname = $this->input->post('gname');
        $g_email = $this->input->post('g_email');
        $grole = $this->input->post('g_role');
        $rsvp = $this->input->post('rsvp');
        $ev_id = $this->input->post('ev-id');

        $data = array(
            'g_name' => $gname,
            'g_email' => $g_email,
            'g_role' => $grole,
            'rsvp' => $rsvp,
            'event_id' => $ev_id
        );
        $this->guest_m->insert_guest_details($data);
        echo json_encode('success');
    }
    
    function get_guests(){
        echo json_encode(array("aaData" => $this->guest_m->get_guests($this->input->post("ev-id"))));
    }

}
